package org.example.quartz;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.domain.StepInfo;
import org.example.domain.SysUser;
import org.example.mone.model.User;
import org.example.serivce.ISysUserService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class TestMyPlusQuartz extends QuartzJobBean {

    @Autowired
    ISysUserService iSysUserService;


    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            User user = new User();
            user.setAge(20);
            user.setEmail("xxx@gmail.com");
            user.setName("测试");
            user.setId(DateUtil.current());
            user.setStepInfo(new StepInfo(1, "one", "第一步"));
            iSysUserService.saveUser(user);
            DruidDataSource dataSource = SpringUtil.getBean("dataSource");
            log.info("insert user:{}", user);

//            IPage<User> iPage = iSysUserService.selectByIPage(2, 10);
//            List<User> list = iPage.getRecords();
//            for (User iUser : list) {
////                log.info("select userByIPage:{}", iUser);
//            }
//            log.info("Mybatis-Plus selectByPage pageNum={},pageSize={},total={},pages={}", iPage.getCurrent(), iPage.getSize(), iPage.getTotal(),iPage.getPages());

            PageInfo<User> pageInfo = iSysUserService.selectByPageHelper(2, 10);

            log.info("Mybatis-Plus selectByPage pageNum={},pageSize={},total={},pages={},user={}", pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList().get(0));
        } catch (Exception e) {
            log.error("TestMyPlusQuartz Job执行失败: " + DateUtil.now(), e);
        }
    }
}
